package com.cloud.alibaba.producer.controller;

import com.cloud.alibaba.producer.event.AppNotifyEvent;
import com.cloud.alibaba.producer.util.ApplicationContextUtil;
import com.cloud.yangfeiself.dto.ResResult;
import com.cloud.yangfeiself.event.DistributeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;

@RestController
public class TestController {


    @Autowired
    private ApplicationEventPublisher publisher;

    @Value("${spring.cloud.bus.id}")
    private String serivceId;

/*    @Autowired
    private RabbitTemplate rabbitTemplate;*/


    @GetMapping(value = "/echo/{string}")
    public String echo(@PathVariable String string) {
        return "Hello Nacos Discovery " + string;
    }

    @GetMapping(value = "/exit")
    public void exit() {
        SpringApplication.exit(ApplicationContextUtil.applicationContext,()->500);
    }

    /**
     *
     *
     * @param appNotifyEvent 事件对象
     * @return
     */
    @RequestMapping(value="/serviceEvent",method= RequestMethod.GET)
    public Object serviceEvent(AppNotifyEvent appNotifyEvent){
        ApplicationContextUtil.applicationContext.publishEvent(appNotifyEvent);
        return "ok";
    }

    /**
     *
     *
     * @param
     * @return
     */
    @RequestMapping(value="/distributeEvent",method= RequestMethod.GET)
    public Object distributeEvent(Integer code,String msg){
        publisher.publishEvent(new DistributeEvent(this,serivceId,"**",code,msg));
        return ResResult.ok();
    }

    @RequestMapping(value="/debug",method=RequestMethod.GET)
    public Object debugContext(){
        System.out.println("-------------华丽的分割线---------------");
        return ResResult.ok();
    }


    @RequestMapping(value="/sendRabbitMsg",method=RequestMethod.GET)
    public Object sendRabbitMsg(String key,
                                String msg){
//        rabbitTemplate.convertSendAndReceive("my_any_exchange",key,msg);
//        rabbitTemplate.convertSendAndReceive("my_any_exchange",key,new ResResult(1,"str",null));
        return ResResult.ok();
    }

    @GetMapping(value = "auth/echo/{string}")
    public String authEcho(@PathVariable String string) {
        return "Hello Nacos Discovery " + string;
    }
}
